package id.net.iconpln.bb.client.view;

import id.net.iconpln.bb.client.model.BeanModelMasterBb;
import id.net.iconpln.bb.client.service.BatubaraService;
import id.net.iconpln.bb.client.service.BatubaraServiceAsync;
import id.net.iconpln.bb.client.service.MasterRPCService;
import id.net.iconpln.bb.client.service.MasterRPCServiceAsync;
import id.net.iconpln.bb.client.util.ClientModule;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.core.FastMap;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CellSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.CheckColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.EditorGrid;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.extjs.gxt.ui.client.widget.toolbar.LabelToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class PageBbMasterPembangkit extends LayoutContainer {
	private final BatubaraServiceAsync rkapService = GWT.create(BatubaraService.class);
	private final MasterRPCServiceAsync masterService = GWT.create(MasterRPCService.class);
	private Grid gridSurveyor, grid, gridkit;
	private EditorGrid gridSurveyorList, gridPemasokList;
	private ListStore storePembangkit, store, storeSurveyor, storeSurveyorList, storePemasokList;
	private Button btnRefrestKit, btupdatePembangkit, btdeletePembangkit;
	private TextField txtnmkit, txtjeniskit, txtdaya, txtpemilik,
		txtalamat, txtlatitude, txtlongitude, txtNmPIC, txttlp, txtsfc, txtcapacity, txtheat;
	private SimpleComboBox txtWilayah;
	private FormPanel formPanelPembangkit;
	
	private Button btnAddSurveyor, btnDelSurveyor, btnSavekSurveyor;
	private Button btnAddPemasok, btnDelPemasok, btnSavePemasok;
	
	private Window windowPembangkit, windowSurveyor, windowPemasok;
	
	private BeanModelMasterBb kitbean = new BeanModelMasterBb();
	ToolBar toolbar = new ToolBar();
	ToolBar toolbarSurveyor = new ToolBar();
	ToolBar toolbar2 = new ToolBar();
	List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
     
	@Override
	protected void onRender(Element parent, int pos) {
	    super.onRender(parent, pos); 
	    setLayout(new FitLayout());  
	    DateTimeFormat dt = DateTimeFormat.getFormat("dd/MM/yyyy");
	    ContentPanel cp = new ContentPanel();  
	    cp.setHeading("Master Pembangkit");  
	    cp.setFrame(true); 
	    cp.setLayout(new RowLayout(Orientation.HORIZONTAL));  
	    
	    gridkit = createGridkit();
	    
	    Button btNew = new Button("New");  
	    btNew.addSelectionListener(new SelectionListener<ButtonEvent>() { 
	      @Override  
	      public void componentSelected(ButtonEvent ce) {  
	    	  windowPembangkit.show();	    	  	
	      }  
	    });
	    btNew.setIcon(IconHelper.createStyle("icon-contact-add"));
	    
	    btupdatePembangkit = new Button("Edit", new SelectionListener<ButtonEvent>() {  
		      @Override  
		      public void componentSelected(ButtonEvent ce) {
		    	  	
		      }  
	    });  
	    btupdatePembangkit.setIcon(IconHelper.createStyle("icon-reza-edit"));	   
	    
	    btdeletePembangkit = new Button("Delete", new SelectionListener<ButtonEvent>() {  
		      @Override  
		      public void componentSelected(ButtonEvent ce) {	
			    	BeanModelMasterBb aa = new BeanModelMasterBb();
			    	aa.setId_pltu(kitbean.getId_pltu());
			  	  	rkapService.insertMasterBB(aa,"PEMBANGKIT","DELETE", new AsyncCallback<List<BeanModelMasterBb>>() {
						public void onFailure(Throwable caught) {
							GWT.log(caught.getMessage(), null);
						}

						public void onSuccess(List<BeanModelMasterBb> result) {
							 gridkit.mask("load Data ....","x-mask-loading");
					    	 storePembangkit.removeAll();
					 		 getdataKit();
						}
					});
			  	  	gridkit.getStore().commitChanges();
		      }  
	    });  
	    btdeletePembangkit.setIcon(IconHelper.createStyle("icon-reza-delete"));
	    
	    btnRefrestKit = new Button("Refresh", new SelectionListener<ButtonEvent>() {  
	    	@Override  
	    	public void componentSelected(ButtonEvent ce) {
	    		getdataKit();  	
	      	}  
	    });  
	    
	    
	    ContentPanel cpPembangkit = new ContentPanel();
	    cpPembangkit.setHeaderVisible(false);
	    cpPembangkit.add(gridkit);
	    cpPembangkit.setLayout(new FitLayout());
	    toolbar.add(new LabelToolItem("Daftar Pembangkit"));
	    toolbar.add(new SeparatorToolItem());
	    toolbar.add(btNew);
		toolbar.add(new SeparatorToolItem());
	    toolbar.add(btupdatePembangkit);
		toolbar.add(new SeparatorToolItem()); 
		toolbar.add(btdeletePembangkit);
		toolbar.add(new SeparatorToolItem()); 
		toolbar.add(btnRefrestKit);
		cpPembangkit.setTopComponent(toolbar);
	    
	    createGridSurveyor();
	    
	    btnAddSurveyor = new Button("Add");  
	    btnAddSurveyor.addSelectionListener(new SelectionListener<ButtonEvent>() { 
	      @Override  
	      public void componentSelected(ButtonEvent ce) {  
	    	  if (!(gridkit.getSelectionModel().getSelectedItem() == null)){
	    		  getdataSurveyorOut(gridkit.getSelectionModel().getSelectedItem().get("id_pltu").toString());
	    	  }
	      }  
	    });
	    btnAddSurveyor.setIcon(IconHelper.createStyle("icon-contact-add"));
	    
	    btnDelSurveyor = new Button("Remove");  
	    btnDelSurveyor.addSelectionListener(new SelectionListener<ButtonEvent>() { 
	      @Override  
	      public void componentSelected(ButtonEvent ce) {  
	    	  setApprovalSurveyor(gridSurveyor.getSelectionModel().getSelectedItem().get("id_surveyor").toString() ,
	  					gridkit.getSelectionModel().getSelectedItem().get("id_pltu").toString(), "DELETE");
	      }  
	    });
	    btnDelSurveyor.setIcon(IconHelper.createStyle("icon-contact-add"));
	    
	    ContentPanel panSurveyor = new ContentPanel();
	    panSurveyor.setHeaderVisible(false);
	    panSurveyor.setLayout(new FitLayout());
	    panSurveyor.add(gridSurveyor);
	    toolbarSurveyor.add(new LabelToolItem("Daftar Surveyor"));
	    toolbarSurveyor.add(new SeparatorToolItem());
	    toolbarSurveyor.add(btnAddSurveyor);
	    toolbarSurveyor.add(new SeparatorToolItem());
	    toolbarSurveyor.add(btnDelSurveyor);
	    panSurveyor.setTopComponent(toolbarSurveyor);
	    
	    createGridPemasok();	
	    
	    btnAddPemasok = new Button("Add");  
	    btnAddPemasok.addSelectionListener(new SelectionListener<ButtonEvent>() { 
	      @Override  
	      public void componentSelected(ButtonEvent ce) {  
	    	  if (!(gridkit.getSelectionModel().getSelectedItem() == null)){
	    		  getdataPemasokOut(gridkit.getSelectionModel().getSelectedItem().get("id_pltu").toString());
	    	  }
	      }  
	    });
	    btnAddPemasok.setIcon(IconHelper.createStyle("icon-contact-add"));
	    
	    btnDelPemasok = new Button("Remove");  
	    btnDelPemasok.addSelectionListener(new SelectionListener<ButtonEvent>() { 
	      @Override  
	      public void componentSelected(ButtonEvent ce) {  
	    	  setApprovalPemasok(grid.getSelectionModel().getSelectedItem().get("id_pemasok").toString() ,
  					gridkit.getSelectionModel().getSelectedItem().get("id_pltu").toString(), "DELETE");
	      }  
	    });
	    btnDelPemasok.setIcon(IconHelper.createStyle("icon-contact-add"));
	    
	    ContentPanel panPemasok = new ContentPanel();
	    panPemasok.setHeaderVisible(false);
	    panPemasok.setLayout(new FitLayout());
	    panPemasok.add(grid);
	    toolbar2.add(new LabelToolItem("Daftar Pemasok"));
	    toolbar2.add(new SeparatorToolItem());
	    toolbar2.add(btnAddPemasok);
	    toolbar2.add(new SeparatorToolItem());
	    toolbar2.add(btnDelPemasok);
	    panPemasok.setTopComponent(toolbar2);
	    
	    ContentPanel panel = new ContentPanel();
	    panel.setHeaderVisible(false);
	    panel.setLayout(new RowLayout(Orientation.VERTICAL));
	    panel.add(panSurveyor, new RowData(1, .5));
	    panel.add(panPemasok, new RowData(1, .5));
	    
	    ContentPanel delimeter = new ContentPanel();
	    delimeter.setHeaderVisible(false);
	    delimeter.setWidth(10);
	    
	    cp.add(cpPembangkit, new RowData(.5, 1));
	    cp.add(delimeter, new FitData(5));
	    cp.add(panel, new RowData(.5, 1));      
	    
	    add(cp, new FitData(5)); 
	    
	    createFormPembangkit();
	  }  
	  
	private Grid createGridkit() { 
		final List<ColumnConfig> columns = new ArrayList<ColumnConfig>(); 	    
		storePembangkit = new ListStore();
		getdataKit(); 
		
		RowNumberer id = new RowNumberer();  
	    id.setWidth(30);
	    
	    ColumnConfig nmkit = new ColumnConfig("nama_pembangkit", "Nama Pembangkit", 250);
	    nmkit.setAlignment(HorizontalAlignment.LEFT);
	    columns.add(nmkit);
	    
	    ColumnConfig nmpic = new ColumnConfig("nama_pic", "Nama PIC", 120);
	    nmpic.setAlignment(HorizontalAlignment.LEFT);
	    columns.add(nmpic);
	    
	    ColumnConfig alamat = new ColumnConfig("alamat", "Alamat", 100);
	    alamat.setAlignment(HorizontalAlignment.LEFT);
	    columns.add(alamat);
	    
	    ColumnConfig pemilik = new ColumnConfig("kepemilikan", "Pemilik", 100);
	    pemilik.setAlignment(HorizontalAlignment.LEFT);
	    columns.add(pemilik);
	    
	    ColumnConfig wilayah = new ColumnConfig("wilayah", "Wilayah", 130);
	    wilayah.setAlignment(HorizontalAlignment.LEFT);
	    columns.add(wilayah);
	    
	    ColumnConfig jeniskit = new ColumnConfig("jenis_pembangkit", "Jenis Pembangkit", 130);
	    jeniskit.setAlignment(HorizontalAlignment.LEFT);
	    columns.add(jeniskit);
	    
	    ColumnConfig tlp = new ColumnConfig("telp", "Telp", 130);
	    tlp.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig daya = new ColumnConfig("daya_terpasang", "Daya Terpasang", 130);
	    daya.setAlignment(HorizontalAlignment.LEFT);
	    columns.add(daya);
	    
	    ColumnConfig sfc = new ColumnConfig("sfc", "SFC", 130);
	    sfc.setAlignment(HorizontalAlignment.LEFT);
	    columns.add(sfc);
	    
	    ColumnConfig capacity_F = new ColumnConfig("capacity_factor", "Capacity Factor", 130);
	    capacity_F.setAlignment(HorizontalAlignment.LEFT);
	    columns.add(capacity_F);
	    
	    ColumnConfig hear_rate = new ColumnConfig("heat_rate", "Heat Rate", 130);
	    hear_rate.setAlignment(HorizontalAlignment.LEFT);
	    columns.add(hear_rate); 
	    
	    final ColumnModel cm = new ColumnModel(columns);
	    gridkit = new Grid(storePembangkit, cm);
	    
	    final CellSelectionModel csm = new CellSelectionModel();
	    gridkit.setSelectionModel(csm);
	    gridkit.addListener(Events.CellClick, new Listener<BaseEvent>() {            
            public void handleEvent(BaseEvent ge) {
            	getdataPemasok(gridkit.getSelectionModel().getSelectedItem().get("id_pltu").toString());            
            	getdataSurveyor(gridkit.getSelectionModel().getSelectedItem().get("id_pltu").toString());
            }
        });
    
        return gridkit;   
	}
	  
	private void createGridPemasok() {      
		store = new ListStore();			
		RowNumberer rn = new RowNumberer();  
	    rn.setWidth(30);
	    columns.add(rn);
	    
	    ColumnConfig pemasok = new ColumnConfig("pemasok", "Pemasok", 150);
	    pemasok.setAlignment(HorizontalAlignment.LEFT);
	    columns.add(pemasok);
	    
	    ColumnConfig alamat = new ColumnConfig("alamat", "Alamat", 150);
	    alamat.setAlignment(HorizontalAlignment.LEFT);
	    columns.add(alamat);
	    
	    ColumnConfig pic_presdir = new ColumnConfig("pic_presdir", "PIC Pres Dir", 150);
	    pic_presdir.setAlignment(HorizontalAlignment.LEFT);
	    columns.add(pic_presdir);
	    
	    ColumnConfig pic_dir_marketing = new ColumnConfig("pic_dir_marketing", "PIC Dir Marketing", 150);
	    pic_dir_marketing.setAlignment(HorizontalAlignment.LEFT);
	    columns.add(pic_dir_marketing);

	    ColumnConfig pic_man_opr = new ColumnConfig("pic_man_opr", "PIC Mgr Operasi", 150);
	    pic_man_opr.setAlignment(HorizontalAlignment.LEFT);
	    columns.add(pic_man_opr);
	    
	    ColumnConfig status_pemasok = new ColumnConfig("status_pemasok", "Status Pemasok", 150);
	    status_pemasok.setAlignment(HorizontalAlignment.LEFT);
	    columns.add(status_pemasok);
	    
	    ColumnConfig nama_tambang = new ColumnConfig("nama_tambang", "Nama Tambang", 150);
	    nama_tambang.setAlignment(HorizontalAlignment.LEFT);
	    columns.add(nama_tambang);
	    
	    ColumnConfig produksi = new ColumnConfig("produksi", "Produksi", 150);
	    produksi.setAlignment(HorizontalAlignment.LEFT);
	    columns.add(produksi);
	    
	    ColumnConfig loading_port = new ColumnConfig("loading_port", "Loading Port", 150);
	    loading_port.setAlignment(HorizontalAlignment.LEFT);
	    columns.add(loading_port);
	    
	    ColumnConfig fasilitas_loading = new ColumnConfig("fasilitas_loading", "Fasilitas Loading", 150);
	    fasilitas_loading.setAlignment(HorizontalAlignment.LEFT);
	    columns.add(fasilitas_loading);
	    
	    ColumnConfig jarak_hauling = new ColumnConfig("jarak_hauling", "Jarak Hauling", 150);
	    jarak_hauling.setAlignment(HorizontalAlignment.LEFT);
	    columns.add(jarak_hauling);
	    
	    ColumnConfig flag_blacklist = new ColumnConfig("flag_blacklist", "Flag Blacklist", 150);
	    flag_blacklist.setAlignment(HorizontalAlignment.LEFT);
	    columns.add(flag_blacklist);
	    
	    ColumnConfig status_aktif = new ColumnConfig("status_aktif", "Status Aktif", 150);
	    status_aktif.setAlignment(HorizontalAlignment.LEFT);
	    columns.add(status_aktif);
	    
	    ColumnConfig keterangan = new ColumnConfig("keterangan", "Keterangan", 150);
	    keterangan.setAlignment(HorizontalAlignment.LEFT);
		columns.add(keterangan);
	  
	    final ColumnModel cm = new ColumnModel(columns);	    
	    grid = new Grid(store, cm);
	    grid.setBorders(true);  
	    
	    final List<ColumnConfig> columnsList = new ArrayList<ColumnConfig>(); 
	    CheckColumnConfig checkColumn = new CheckColumnConfig("selectPemasok", "Select", 55); 
	    CheckBox cb = new CheckBox();
	    cb.setValue(true);
	    CellEditor checkBoxEditor = new CellEditor(cb);  
	    checkColumn.setEditor(checkBoxEditor);  
	    columnsList.add(checkColumn);
	    columnsList.add(pemasok);
	    final ColumnModel cmList = new ColumnModel(columnsList);
	    
	    btnSavePemasok = new Button("Save");
	    btnSavePemasok.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				for (int x=0 ; x < gridPemasokList.getStore().getModels().size(); x++){
	        		String v = "";
	        		try{
	        			v = gridPemasokList.getStore().getAt(x).get("selectPemasok").toString();
	        		}catch (Exception e) {
						v = "";
					}
	        		if (v.equals("true")){
	        			setApprovalPemasok(gridPemasokList.getStore().getAt(x).get("id_pemasok").toString() ,
	        					gridkit.getSelectionModel().getSelectedItem().get("id_pltu").toString(), "CREATE");
	        		}
	        		if ( x == gridPemasokList.getStore().getModels().size() - 1){
	        			getdataPemasok(gridkit.getSelectionModel().getSelectedItem().get("id_pltu").toString());        			
	        		}
	        	}
				
				windowPemasok.hide();
			}
		});
	    ToolBar toolbarListPemasok = new ToolBar();
	    toolbarListPemasok.add(btnSavePemasok);
	    
	    storePemasokList = new ListStore();
	    gridPemasokList = new EditorGrid(storePemasokList, cmList);
	    gridPemasokList.setBorders(true); 
	    gridPemasokList.setHeight(280);
	    
	    windowPemasok = new Window();  
	    windowPemasok.setHeading("List Pemasok");
	    windowPemasok.setSize(400, 300);
	    windowPemasok.add(gridPemasokList);
	    windowPemasok.setTopComponent(toolbarListPemasok);
	}  
		
	private void setApprovalPemasok(String id_pemasok, String id_pltu, final String crud){
		Map mapParam = new FastMap();
	  	mapParam.put("id_pemasok", id_pemasok);
	  	mapParam.put("id_pltu", id_pltu);
	  	mapParam.put("id_surveyor", "");
	  	mapParam.put("user", "DHAMARSU");
	  	mapParam.put("app_access", "WEB");
	  	mapParam.put("crud", crud);
	  
	  	masterService.setRelationMaster("PMSOKKIT", mapParam,new AsyncCallback<List<Map>>() {
	  		public void onFailure(Throwable caught) {
	  			GWT.log(caught.getMessage(), null);
	  		}

	  		public void onSuccess(List<Map> result) {	
	  			if (crud.equals("DELETE")){
	  				MessageBox.alert("Information", result.get(0).get("message").toString(), null);
	  			}
	  		}
	  	});
	}
	
	private void setApprovalSurveyor(String id_surveyor, String id_pltu, final String crud){
		Map mapParam = new FastMap();
	  	mapParam.put("id_pemasok", "");
	  	mapParam.put("id_pltu", id_pltu);
	  	mapParam.put("id_surveyor", id_surveyor);
	  	mapParam.put("user", "DHAMARSU");
	  	mapParam.put("app_access", "WEB");
	  	mapParam.put("crud", crud);
	  
	  	masterService.setRelationMaster("YORKIT", mapParam,new AsyncCallback<List<Map>>() {
	  		public void onFailure(Throwable caught) {
	  			GWT.log(caught.getMessage(), null);
	  		}

	  		public void onSuccess(List<Map> result) {	
	  			if (crud.equals("DELETE")){
	  				MessageBox.alert("Information", result.get(0).get("message").toString(), null);
	  			}
	  		}
	  	});
	}
	
	private void createGridSurveyor() { 
		final List<ColumnConfig> columns = new ArrayList<ColumnConfig>(); 		
		storeSurveyor = new ListStore();		
		RowNumberer id = new RowNumberer();  
	    id.setWidth(30);
	    columns.add(id);
	    
	    ColumnConfig idsvr = new ColumnConfig("id_surveyor", "ID Surveyor", 120);
	    idsvr.setAlignment(HorizontalAlignment.LEFT);	    
	    
	    ColumnConfig nmsvr = new ColumnConfig("nama_surveyor", "Nama Surveyor", 250);
	    nmsvr.setAlignment(HorizontalAlignment.LEFT);
	    columns.add(nmsvr);	    
	    
	    ColumnConfig nokerjasama = new ColumnConfig("no_kerjasama", "No Kerja Sama", 100);
	    nokerjasama.setAlignment(HorizontalAlignment.RIGHT);
	    columns.add(nokerjasama);
	    
	    ColumnConfig tgl = new ColumnConfig("tanggal", "Tanggal", 100);
	    tgl.setAlignment(HorizontalAlignment.LEFT);
	    columns.add(tgl);
	    
	    ColumnConfig tlp = new ColumnConfig("telp", "Telp", 130);
	    tlp.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig alamat = new ColumnConfig("alamat", "Alamat", 130);
	    alamat.setAlignment(HorizontalAlignment.LEFT);
	    columns.add(alamat);
	    
	    ColumnConfig pic = new ColumnConfig("pic", "PIC", 130);
	    pic.setAlignment(HorizontalAlignment.LEFT);
	    columns.add(pic);
	    
	    ColumnConfig ket = new ColumnConfig("keterangan", "Keterangan", 130);
	    ket.setAlignment(HorizontalAlignment.LEFT);
	    columns.add(ket);
	    
	    ColumnConfig nokontrak = new ColumnConfig("no_kontrak", "No Kontrak", 130);
	    nokontrak.setAlignment(HorizontalAlignment.LEFT);
	    columns.add(nokontrak);	    	    
	    
	    final ColumnModel cm = new ColumnModel(columns);	    
	    gridSurveyor = new Grid(storeSurveyor, cm);
	    gridSurveyor.setBorders(true);  	    
	    
	    final List<ColumnConfig> columnsList = new ArrayList<ColumnConfig>(); 
	    CheckColumnConfig checkColumn = new CheckColumnConfig("selectSurveyor", "Select", 55); 
	    CheckBox cb = new CheckBox();
	    cb.setValue(true);
	    CellEditor checkBoxEditor = new CellEditor(cb);  
	    checkColumn.setEditor(checkBoxEditor);  
	    columnsList.add(checkColumn);
	    columnsList.add(nmsvr);
	    final ColumnModel cmList = new ColumnModel(columnsList);
	    
	    btnSavekSurveyor = new Button("Save");
	    btnSavekSurveyor.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				for (int x=0 ; x < gridSurveyorList.getStore().getModels().size(); x++){
	        		String v = "";
	        		try{
	        			v = gridSurveyorList.getStore().getAt(x).get("selectSurveyor").toString();
	        		}catch (Exception e) {
						v = "";
					}
	        		if (v.equals("true")){
	        			setApprovalSurveyor(gridSurveyorList.getStore().getAt(x).get("id_surveyor").toString() ,
	        					gridkit.getSelectionModel().getSelectedItem().get("id_pltu").toString(), "CREATE");
	        		}
	        		if ( x == gridSurveyorList.getStore().getModels().size() - 1){
	        			getdataSurveyor(gridkit.getSelectionModel().getSelectedItem().get("id_pltu").toString());        			
	        		}
	        	}				
				windowSurveyor.hide();
			}
		});
	    ToolBar toolbarListSurveyor = new ToolBar();
	    toolbarListSurveyor.add(btnSavekSurveyor);
	    
	    storeSurveyorList = new ListStore();
	    gridSurveyorList = new EditorGrid(storeSurveyorList, cmList);
	    gridSurveyorList.setBorders(true); 
	    gridSurveyorList.setHeight(280);
	    
	    windowSurveyor = new Window();  
	    windowSurveyor.setHeading("List Surveyor");
	    windowSurveyor.setSize(400, 300);
	    windowSurveyor.add(gridSurveyorList);
	    windowSurveyor.setTopComponent(toolbarListSurveyor);
	}  

	private void getdataKit() {
		masterService.getMaster("KIT", new FastMap(), new AsyncCallback<List<Map>>() {
			public void onFailure(Throwable caught) {
				GWT.log(caught.getMessage(), null);
			}

			public void onSuccess(List<Map> result) {	
				storePembangkit.removeAll();
				storePembangkit.add(ClientModule.setListBaseModelFromListMap(result));
				
				gridkit.unmask();
			}
		});
	};

	private void getdataSurveyor(String id_pltu) {
		Map map = new FastMap();
		map.put("id_pltu", id_pltu);
		masterService.getMaster("SURVEYOR_BY_PLTU", map , new AsyncCallback<List<Map>>() {
			public void onFailure(Throwable caught) {
				GWT.log(caught.getMessage(), null);
			}

			public void onSuccess(List<Map> result) {	
				storeSurveyor.removeAll();
				storeSurveyor.add(ClientModule.setListBaseModelFromListMap(result));
			}
		});
	}
	
	private void getdataSurveyorOut(String id_pltu) {
		Map map = new FastMap();
		map.put("id_pltu", id_pltu);
		masterService.getMaster("SURVEYOR_BY_PLTU_OUT", map , new AsyncCallback<List<Map>>() {
			public void onFailure(Throwable caught) {
				GWT.log(caught.getMessage(), null);
			}

			public void onSuccess(List<Map> result) {	
				storeSurveyorList.removeAll();
				storeSurveyorList.add(ClientModule.setListBaseModelFromListMap(result));
				
				windowSurveyor.show();
			}
		});
	}
	
	private void getdataPemasokOut(String id_pltu) {
		Map map = new FastMap();
		map.put("id_pltu", id_pltu);
		masterService.getMaster("PEMASOK_BY_PLTU_OUT", map , new AsyncCallback<List<Map>>() {
			public void onFailure(Throwable caught) {
				GWT.log(caught.getMessage(), null);
			}

			public void onSuccess(List<Map> result) {	
				storePemasokList.removeAll();
				storePemasokList.add(ClientModule.setListBaseModelFromListMap(result));
				
				windowPemasok.show();
			}
		});
	}
	  
	private void getdataPemasok(String id_pltu) {
		Map map = new FastMap();
		map.put("id_pltu", id_pltu);
		masterService.getMaster("PEMASOK_BY_PLTU", map , new AsyncCallback<List<Map>>() {
			public void onFailure(Throwable caught) {
				GWT.log(caught.getMessage(), null);
			}

			public void onSuccess(List<Map> result) {	
				store.removeAll();
				store.add(ClientModule.setListBaseModelFromListMap(result));
			}
		});
	}

	private void createFormPembangkit(){
		ContentPanel simple = new ContentPanel();  
		simple.setHeaderVisible(false);
		simple.setFrame(true); 
		simple.setWidth(400);
		simple.setLayout(new RowLayout(Orientation.HORIZONTAL)); 
		
		formPanelPembangkit= new FormPanel();	    
		formPanelPembangkit.setHeaderVisible(false);
	    
		FieldSet fieldSet = new FieldSet();  
		fieldSet.setHeading("Identitas Pembangkit");  
		fieldSet.setWidth(380);

		FormLayout layout = new FormLayout();  
		layout.setLabelWidth(100);  
		fieldSet.setLayout(layout);

		FieldSet fieldSet1 = new FieldSet();  
		fieldSet1.setWidth(380);
		fieldSet1.setHeading("Informasi Pembangkit");  

		FormLayout layout1 = new FormLayout();  
		layout1.setLabelWidth(100); 
		fieldSet1.setLayout(layout1);		
		;

		txtnmkit = new TextField(); 
		txtnmkit.setFieldLabel("Nama Pembangkit"); 
		fieldSet.add(txtnmkit);  
		
		txtjeniskit = new TextField(); 
		txtjeniskit.setFieldLabel("Jenis Pembangkit"); 
		fieldSet.add(txtjeniskit);  

		txtdaya = new TextField(); 
		txtdaya.setFieldLabel("Daya Terpasang"); 
		fieldSet.add(txtdaya);  

		txtpemilik = new TextField(); 
		txtpemilik.setFieldLabel("Pemilik");
		fieldSet.add(txtpemilik);  

		txtalamat = new TextField(); 
		txtalamat.setFieldLabel("Alamat"); 
		txtalamat.setMaxLength(100);
		fieldSet.add(txtalamat);  

		txtWilayah = new SimpleComboBox();  
		txtWilayah.setFieldLabel("Wilayah");  
		txtWilayah.setForceSelection(true);  
		txtWilayah.setTypeAhead(true);   
		txtWilayah.setTriggerAction(TriggerAction.ALL);
		txtWilayah.add("IB");
		txtWilayah.add("IT");
		txtWilayah.add("JB");
		fieldSet.add(txtWilayah);
		
		txtlatitude = new TextField(); 
		txtlatitude.setFieldLabel("Latitude"); 
		txtlatitude.setMaxLength(100);
		fieldSet.add(txtlatitude);

		txtlongitude = new TextField(); 
		txtlongitude.setFieldLabel("Longitude"); 
		txtlongitude.setMaxLength(100);
		fieldSet.add(txtlongitude);

		txtNmPIC = new TextField(); 
		txtNmPIC.setFieldLabel("Nama PIC"); 
		txtNmPIC.setMaxLength(100);
		fieldSet1.add(txtNmPIC);
		
		txttlp = new TextField(); 
		txttlp.setFieldLabel("Telp"); 
		txttlp.setMaxLength(100);
		fieldSet1.add(txttlp);

		txtsfc = new TextField(); 
		txtsfc.setFieldLabel("SFC"); 
		txtsfc.setMaxLength(100);	
		fieldSet1.add(txtsfc); 

		txtcapacity = new TextField(); 
		txtcapacity.setFieldLabel("Capacity Factor"); 
		txtcapacity.setMaxLength(100);	
		fieldSet1.add(txtcapacity); 

		txtheat = new TextField(); 
		txtheat.setFieldLabel("Heat Rate"); 
		txtheat.setMaxLength(100);
		fieldSet1.add(txtheat);   
	    
		formPanelPembangkit.add(fieldSet);
		formPanelPembangkit.add(fieldSet1);
		
		formPanelPembangkit.addButton(new Button("Save", new SelectionListener<ButtonEvent>() {  
		      @Override  
		      public void componentSelected(ButtonEvent ce) {
		    	  saveDataPembangkit();
		      }
		}));
		
		formPanelPembangkit.setButtonAlign(HorizontalAlignment.LEFT);  
		formPanelPembangkit.addButton(new Button("Cancel", new SelectionListener<ButtonEvent>() {  
	      @Override  
	      public void componentSelected(ButtonEvent ce) {  
	    	windowPembangkit.hide();
	      }  
	    }));  
		simple.add(formPanelPembangkit);
	    windowPembangkit = new Window();  
	    windowPembangkit.setHeading("New Master Pembangkit");
	    windowPembangkit.setWidth(400);
	    windowPembangkit.add(simple);
	    
	}
	
	private void saveDataPembangkit(){
		String session = Registry.get("isModule");  	
    	BeanModelMasterBb aa = new BeanModelMasterBb();
        aa.setNama_pembangkit(txtnmkit.getRawValue());
        aa.setNama_pic(txtNmPIC.getRawValue());
        aa.setAlamat(txtalamat.getRawValue());
        aa.setKepemilikan(txtpemilik.getRawValue());
        aa.setLatitude(txtlatitude.getRawValue());
        aa.setLongitude(txtlongitude.getRawValue());
        aa.setWilayah(txtWilayah.getRawValue());
        aa.setJenis_pembangkit(txtjeniskit.getRawValue());
        aa.setTelp(txttlp.getRawValue());
        aa.setDaya_terpasang(txtdaya.getRawValue());
        aa.setSfc(txtsfc.getRawValue());
        aa.setCapacity_factor(txtcapacity.getRawValue());
        aa.setHeat_rate(txtheat.getRawValue());
        aa.setCreateby(session);
        aa.setApp_access("WEB App");
        
        rkapService.insertMasterBB(aa,"PEMBANGKIT","CREATE", new AsyncCallback<List<BeanModelMasterBb>>() {
			public void onFailure(Throwable caught) {
				GWT.log(caught.getMessage(), null);
			}

			public void onSuccess(List<BeanModelMasterBb> result) {
				 gridkit.mask("load Data ....","x-mask-loading");
		 		 getdataKit();
		 		 
		 		 Info.display("Notifikasi: ", "Data Berhasil Di simpan");
			     windowPembangkit.hide();
			     formPanelPembangkit.reset();
			}
		});
	}
		  
}

